####################################################################
## author:    Robert A. Huber, Lukas P. Fesenfeld & Thomas Bernauer
## contact:   robert.huber@ir.gess.ethz.ch
## file name: erc_popcli_export.R
## Context:   ERC Populism Project on Public Support for Climate Politics
## started:   2018-03-07
## Summary:   runs R-Scripts
######################################################################

# Descriptive Statistics ####


varList <- c("climConcern", "cliAct", "cliEco", "wtp", "wtp1", "wtp2", "wtp3", "wtp4", "wtp5", "age", "gender", "educGrp", "inc", "urbanRural", "partyID",  "pop", "peop", "trt", "anti", "man", "ccKnow", "region", "employGrp")

df_ds <- df[,varList]
df_ds <- subset(df_ds, trt != "Total Control")

p_cliConcern <- ggplot(df_ds, aes(climConcern)) +
  geom_histogram(bins = 20) +
  labs(y="Count", x= "Climate Concern") +
  theme_tufte() +
  theme(text = element_text(size=18))

p_cliEco <- ggplot(df_ds, aes(cliEco)) +
  geom_bar(stat = "count") +
  labs(y="Count", x= "Climate Policy vis-a-vis Economic Growth") +
  theme_tufte()+
  ggtitle("Some believe that measures against climate change, such as reducing carbon dioxide emissions from\nburning oil, gas and coal, should have priority even if that hurts the economy and jobs. Others\nbelieve that the economy and jobs should have priority even if that results in more climate change.\nWhat is your opinion?") + 
  scale_x_continuous(breaks = 1:7, labels = c("The economy and jobs\nshould have priority", 2:6, "Measures against climate change\nshould have priority"))  +
  theme(text = element_text(size=18))

p_cliAct <- ggplot(df_ds, aes(cliAct)) +
  geom_bar(stat = "count") +
  labs(y="Count", x= "Support for Climate Action") +
  ggtitle("To deal with climate change (global warming), do you think the U.S. government is doing...") +
  theme_tufte() +
  theme(text = element_text(size=18))

p_wtp <- ggplot(df_ds, aes(wtp)) +
  geom_histogram(bins =15) +
  labs(y="Count", x= "Willingness to Pay") +
  ggtitle("Willingness To Pay -- Distribution of Factor Score from Five Questions") + 
  theme_tufte() +
  theme(text = element_text(size=18))

p_age <- ggplot(df_ds, aes(age)) +
  geom_histogram(bins = 25) +
  labs(y="Count", x= "Age in years") +
  ggtitle("What is your age?") +
  theme_tufte() +
  theme(text = element_text(size=18))
  
p_gender <- ggplot(df_ds, aes(gender)) +
  geom_bar(stat = "count") +
  labs(y="Count", x= "Gender ") +
  ggtitle("What is your gender?") +
  theme_tufte() +
  theme(text = element_text(size=18))

p_edu <- ggplot(df_ds, aes(educGrp)) +
  geom_bar(stat = "count")  +
  labs(y="Count", x= "Education") +
  ggtitle("What is the highest level of education you have completed?") +
  theme_tufte() +
  theme(text = element_text(size=18))

p_inc <- ggplot(df_ds, aes(inc)) +
  geom_bar(stat = "count")  +
  labs(y="Count", x= "Income") + 
  ggtitle("2017 Annual Household Income Before Taxes") + 
  theme_tufte() +
  theme(text = element_text(size=18))

p_urbanRural <- ggplot(df_ds, aes(urbanRural)) +
  geom_bar(stat = "count")  +
  labs(y="Count", x= "Living Conditions") +
  ggtitle("Geographic Information Recoded into Urban and Rural") +
  theme_tufte() +
  theme(text = element_text(size=18))

p_partyID <- ggplot(df_ds, aes(partyID)) +
  geom_bar(stat = "count")  +
  labs(y="Count", x= "Party Identification") +
  theme_tufte() +
  scale_x_discrete("Count", labels = c("D" = "Democrat",
                                       "R" = "Republican",
                                       "I" = "Independent",
                                       "Something else" = "Something else",
                                       "Prefer not to answer" = "Prefer not to answer")) +
  ggtitle("Do you usually think of yourself as a Democrat, a Republican, an Independent, or something else?")  +
  theme(text = element_text(size=18))
                                     
p_pop <- ggplot(df_ds, aes(pop)) +
  geom_histogram(bins = 25) +
  labs(y="Count", x= "Populist Attitudes") + 
  ggtitle("Populism -- Centred Geometric Mean of Three Dimension's Factor Scores") + 
  theme_tufte() +
  theme(text = element_text(size=18))

p_peop <-  ggplot(df_ds, aes(peop)) +
  geom_histogram(bins = 20) +
  labs(y="Count", x= "People-Centrism") +
  ggtitle("People-Centrism -- Factor Score from Three Questions") + 
  theme_tufte() +
  theme(text = element_text(size=18))

p_anti <-  ggplot(df_ds, aes(anti)) +
  geom_histogram(bins = 20) +
  labs(y="Count", x= "Anti-Elitism") +
  ggtitle("Anti-Elitism -- Factor Score from Three Questions") + 
  theme_tufte() +
  theme(text = element_text(size=18))

p_man <-  ggplot(df_ds, aes(man)) +
  geom_histogram(bins = 20) +
  labs(y="Count", x= "Manichean Discourse") +
  ggtitle("Manichean Discourse -- Factor Score from Three Questions") + 
  theme_tufte() +
  theme(text = element_text(size=18))

p_ccknow <- ggplot(df_ds, aes(ccKnow)) +
  geom_bar(stat = "count")  +
  labs(y="Count", x= "Knowledge about climate change") +
  theme_tufte() +
  ggtitle("Could you explain climate change to a friend?")  +
  theme(text = element_text(size=18))

p_region <- ggplot(df_ds, aes(region)) +
  geom_bar(stat = "count")  +
  labs(y="Count", x= "Region") +
  theme_tufte() +
  ggtitle("Region")  +
  theme(text = element_text(size=18))

p_employ <- ggplot(df_ds, aes(employGrp)) +
  geom_bar(stat = "count")  +
  labs(y="Count", x= "Employment Status") +
  theme_tufte() +
  ggtitle("Which statement best describes your current employment status?")  +
  theme(text = element_text(size=18))


list_plots <- c("p_cliConcern", "p_cliAct", "p_cliEco", "p_wtp", "p_age", "p_edu", "p_gender", "p_inc", "p_partyID", "p_peop", "p_pop", "p_urbanRural", "p_anti", "p_man", "p_ccknow", "p_region", "p_employ")

for (i in 1:length(list_plots)){
  
  a <- get(list_plots[i])
  ggsave(filename = paste0("./figures/descriptives/", list_plots[i], ".pdf", sep=""), 
         plot = a,
         width = 20,
         height = 12,
         units = "cm",
         device = "pdf")
  ggsave(filename = paste0("./figures/descriptives/", list_plots[i], ".jpg", sep=""), 
         plot = a,
         width = 20,
         height = 12,
         units = "cm",
         device = "jpg")
}

# # Export Regression Results ####

texreg(list(m_Con_ID, m_cliEco_ID, m_wtp_ID),
       caption = "Populism, Political Ideology and Climate Attitudes",
       caption.above = T,
       label = "PCPol_t2",
       stars = c(0.01, 0.05, 0.1),
       override.se = list(m_Con_ID$std.error, m_cliEco_ID$std.error, m_wtp_ID$std.error),
       override.pvalues = list(m_Con_ID$p.value, m_cliEco_ID$p.value, m_wtp_ID$p.value),
       include.ci = F,
       leading.zero = T,
       single.row = F,
       custom.coef.names = c(NA, "High Responsiveness Climate", "High Responsiveness General",
                             "Low Responsiveness Climate", "Low Responsiveness General",
                             "Republican", "Independents", "Something else",
                             "Prefer not to answer", "Populism", "Age",
                             "Gender (Ref = Female)", "Education (Some College)",
                             "Education (College +)", "Income (USD 22501 - USD 43500)",
                             "Income (USD 43501 - USD 72000)", "Income (USD 72000 - USD 117000)",
                             "Income (USD 117001 - USD 214500)", "Income (more than USD 214500)",
                             "Living Conditions (Ref = Urban)", "Climate Knowledge (to some extent)",
                             "Climate Knowledge (No)", "Region (Midwest)",  "Region (South)",
                             "Region (West)", "Employment Status (Working)", 
                             "Hi Resp. Cli. x R", "Hi Resp. Gen. x R", 
                             "Lo Resp. Cli. x R", "Lo Resp. Gen. x R",
                             "Hi Resp. Cli. x I", "Hi Resp. Gen. x I", 
                             "Lo Resp. Cli. x I", "Lo Resp. Gen. x I",
                             "Hi Resp. Cli. x SE", "Hi Resp. Gen. x SE", 
                             "Lo Resp. Cli. x SE", "Lo Resp. Gen. x SE",
                             "Hi Resp. Cli. x No Answer", "Hi Resp. Gen. x No Answer", 
                             "Lo Resp. Cli. x No Answer", "Lo Resp. Gen. x No Answer",
                             "Hi Resp. Cli. x Pop.", "Hi Resp. Gen. x Pop.", 
                             "Lo Resp. Cli. x Pop.", "Lo Resp. Gen. x Pop.",
                             "R x Pop", "I x Pop.", "SE x Pop.", "No Answer x Pop.",
                             "Hi Resp. Cli. x R x Pop.", "Hi Resp. Gen. x R x Pop.", 
                             "Lo Resp. Cli. x R x Pop.", "Lo Resp. Gen. x R x Pop.",
                             "Hi Resp. Cli. x I x Pop.", "Hi Resp. Gen. x I x Pop.", 
                             "Lo Resp. Cli. x I x Pop.", "Lo Resp. Gen. x I x Pop.",
                             "Hi Resp. Cli. x SE x Pop.", "Hi Resp. Gen. x SE x Pop.", 
                             "Lo Resp. Cli. x SE x Pop.", "Lo Resp. Gen. x SE x Pop.",
                             "Hi Resp. Cli. x No Answer x Pop.", "Hi Resp. Gen. x No Answer x Pop.", 
                             "Lo Resp. Cli. x No Answer x Pop.", "Lo Resp. Gen. x No Answer x Pop."),
       longtable = T,
       custom.model.names = c("Climate Concern","Climate Policy vis-a-vis Economic Growth", "WTP"))

###END OF SCRIPT ###